<template>
  <div class="app-container">
    <el-form label-width="70px" :inline="true">
      <el-form-item label="关 键 字">
        <el-input
          v-model="searchObj.keyword"
          placeholder="用户名/姓名/手机号码"
          size="mini"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="fetchData()"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetData"
          >重置</el-button
        >
        <el-button
          type="success"
          icon="el-icon-plus"
          size="mini"
          @click="addOrUpdateHandle()"
          >添 加</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      stripe
      border
      style="width: 100%; margin-top: 10px"
    >
      <el-table-column prop="id" label="序号" width="70" align="center" />

      <el-table-column prop="username" label="用户名" width="180" />
      <el-table-column prop="name" label="姓名" width="110" />
      <el-table-column prop="phone" label="手机" />
      <el-table-column label="是否禁用" width="80">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            @change="switchStatus(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />

      <el-table-column label="操作" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="addOrUpdateHandle(scope.row.id)"
            >修改</el-button
          >
          <el-button
            type="danger"
            size="mini"
            @click="removeDataById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :total="total"
      :page-size="pageSize"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
    />

    <!-- 弹窗, 新增 / 修改 -->
    <AddDialog
      v-if="dialogVisible"
      ref="addOrUpdate"
      @refreshDataList="fetchData"
    ></AddDialog>
  </div>
</template>
<script>
import UserApi from "@/api/system/user";
import AddDialog from "./add.vue";

export default {
  components: { AddDialog },
  data() {
    return {
      listLoading: false, // 数据是否正在加载
      list: null, // 列表
      total: 0, // 数据库中的总记录数
      pageIndex: 1, // 默认页码
      pageSize: 10, // 每页记录数
      searchObj: {}, // 查询表单对象
      dialogVisible: false,
      sysUser: {},
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    //更改用户状态
    switchStatus(row) {
      let { id, status } = row;
      UserApi.updateUser({ id, status }).then((response) => {
        this.$message.success(response.message);
        this.fetchData();
      });
    },
    //删除
    removeDataById(id) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //调用方法删除
          UserApi.removeUser({ id }).then((response) => {
            //提示
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            //刷新
            this.fetchData();
          });
        })
        .catch(() => {});
    },
    // 重置查询表单
    resetData() {
      this.searchObj = {};
      this.pageIndex = 1;
      this.fetchData();
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.fetchData();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.fetchData();
    },
    //列表
    fetchData() {
      UserApi.getPageList({
        pageIndex: this.pageIndex,
        pageSize: this.pageSize,
        ...this.searchObj,
      }).then((response) => {
        this.list = response.data.list;
        this.total = response.data.totalCount;
      });
    },
  },
};
</script>
